<?php

namespace addons\groupon\controller\store;

use addons\groupon\exception\Exception;
use addons\groupon\library\traits\export\ExportOrder;
use app\admin\model\groupon\store\Upkeeplog;

/**
 * 自提点包裹
 */
class StoreExpress extends Base
{
    use ExportOrder;

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $params = $this->request->get();

//        $this->success('包裹列表', \addons\groupon\model\store\StoreExpress::getList($params));
        $this->success('服务列表', \addons\groupon\model\store\UpkeepLog::getList($params));
    }


    public function detail()
    {
        $params = $this->request->get();

        $this->success('包裹详情', \addons\groupon\model\store\StoreExpress::getDetail($params));
    }


    /**
     * 包裹到货
     *
     * @return void
     */
    public function arrive()
    {
        $params = $this->request->post();
        $this->success('到货成功', \addons\groupon\model\store\StoreExpress::operArrive($params));
    }

    /**
     * @Author:zhu
     */
    public function startService()
    {
        $params = $this->request->post();
        \addons\groupon\model\store\UpkeepLog::startService($params);
        $this->success('操作成功');
    }

    /**
     * @Author:zhu
     * 保养结束提交申请
     */
    public function apply()
    {

        $params = $this->request->post();
        if (empty($params['images']) || empty($params['phone']) || empty($params['realname']) || empty($params['id'])) {
            $this->error('缺少参数');
        }
        $info = Upkeeplog::where(['id' => $params['id']])->find();
        if (!$info) {
            $this->error('数据不存在');
        }
        if($info->status == 2 || $info->status == 5){
            if (!empty($params['remark'])) {
                $info->remark = $params['remark'];
            }
            $info->status   = 3;
            $info->images   = implode(',', $params['images']);
            $info->phone    = $params['phone'];
            $info->realname = $params['realname'];
            $info->save();
            $this->success('ok');
        }
        $this->error('提交失败');
    }

    public function export()
    {
        $params = $this->request->get();
        $id     = $params['id'] ?? 0;

        $orderStoreExpress = \app\admin\model\groupon\order\OrderStoreExpress::with(['store'])->where('id',
            $id)->find();

        if (!$orderStoreExpress) {
            $this->error('包裹未找到');
        }

        $this->exportOutput($orderStoreExpress);
    }
}
